﻿namespace NTLib.Core.UTest.ScopeLock
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using NTLib.Core.ScopeLock;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    [TestClass]
    public class ScopeLockItemTest
    {
        #region Fields

        private TestContext testContextInstance;

        #endregion

        #region properties

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #endregion

        #region Methods

        [TestCategory("Core.ScopeLock")]
        [TestMethod]
        public void ScopLockItemTest_Default()
        {
            string testA = "test";
            string testb = " is ";
            string testDispose = "dispose";
            StringBuilder builder = new StringBuilder();
            builder.Append(testA);

            using (var item = new ScopeLockItem<StringBuilder>(builder, (b) => b.Append(testDispose)))
            {
                builder.Append(testb);
            }

            Assert.AreEqual(builder.ToString(), testA + testb + testDispose);
        }

        [TestCategory("Core.ScopeLock")]
        [TestMethod]
        public void ScopLockItemTest_Exception()
        {
            string testA = "test";
            string testb = " is ";
            string testDispose = "dispose";
            StringBuilder builder = new StringBuilder();
            builder.Append(testA);

            try
            {
                using (var item = new ScopeLockItem<StringBuilder>(builder, (b) => b.Append(testDispose)))
                {
                    builder.Append(testb);
                    throw new Exception();
                }
            }
            catch
            {
                Assert.AreEqual(builder.ToString(), testA + testb + testDispose);
            }
        }


        #endregion
    }
}
